package com.pubpage.sys_upload_file;

import com.pub.database.*;
import java.util.*;
import javax.servlet.http.*;
import com.pub.web.*;

public class Action {
   QueryBean gqbnDAO=null;
   @SuppressWarnings("unchecked")
public String action(javax.servlet.jsp.PageContext pageContext){
       HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
          HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();

          View view=(View)request.getAttribute("view");
          (new WebFunction()).setPortal_to_session(request); //set session from portal
          if (request.getSession().getAttribute("u_id")==null) {
             try{
                request.getRequestDispatcher((new StringBuffer("login.jsp")).insert(0,"/").toString()).forward(request,response);
                return "login";
             }
             catch(Exception le){
                view.setOnloadScript("can not redirect to login page...please contact IT");
                return "";
             }
          }

          view.setloginUser(request.getSession().getAttribute("u_id").toString());
          String lstrUserNO = request.getSession().getAttribute("u_id").toString();
          Hashtable<String,String> lhtAttInfo = new Hashtable<String,String>();
          if(request.getSession().getAttribute("att_req_info")!=null){
              lhtAttInfo = (Hashtable<String,String>) request.getSession().
                                     getAttribute("att_req_info");
              view.setDB_Alias(lhtAttInfo.get("DB_Alias").toString());
              view.setDBDebug(Boolean.valueOf(lhtAttInfo.get("DBDebug").toString()));
              view.setSourceCharset(lhtAttInfo.get("SourceCharset").toString());
              view.setTargetCharset(lhtAttInfo.get("TargetCharset").toString());

          }
          gqbnDAO=new QueryBean(view.getDB_Alias(),view.getDBDebug(),view.getSourceCharset(),view.getTargetCharset());

          DataView DataView= new DataView(gqbnDAO);
          DataViewForm DataViewForm = new DataViewForm();

          view.setDataView(DataView);
          view.setDataViewForm(DataViewForm);
          (new WebFunction()).setPortal_to_session(request); //set session from portal
          request.setAttribute("DataView",DataView);//讓前端可以呼叫dataview
          request.setAttribute("DataViewForm",DataViewForm);
          Business business = new Business(gqbnDAO);


      try{
          if(lhtAttInfo!=null){
              business.getAttInfoFromSession(view,lhtAttInfo);
          }
          if(request.getContentType()!=null && request.getContentType().indexOf("multipart")>=0){
              business.getFileUploadData(view,request);
          }
          business.setInitValue(view);
          if(view.getprgAction()!=null && (view.getprgAction().equals("upload"))){
              business.exeInsSQL(view);
          }
          if(view.getatt_ifrm_name()!=null){
              //表示按離開鍵的時候要reload opener
              view.setOnExitScript("javascript:opener."+view.getatt_ifrm_name()+".document.forms[0].submit();opener=null;window.close();");
          }else{
              view.setOnExitScript("javascript:opener=null;window.close();");
          }
         if(view.getprgAction()!=null && view.getprgAction().equals("getData")){//利用AJAX即時讀取資料
             String lstrResult = null;
             lstrResult=business.getData(view,lstrUserNO);

             pageContext.getOut().clear();
             pageContext.getOut().write(lstrResult);
             pageContext.getOut().flush();
             return "login";
           }
          if (view.getflow_num()!=null && !view.getflow_num().equals("") && !view.getflow_num().equals("null"))
          {
              view.setDataViewHtml(business.getData(view,lstrUserNO));
            }
         return "";
      }
      catch(Exception e){
         view.setOnloadScript("alert('Program error please contact IT.');");
         return "";
      }
      finally{
         try{
            if(gqbnDAO!=null){
               gqbnDAO.close();
            }
         }
         catch(Exception e){
            System.out.println("can not close querybean in Action.java");
         }
      }
   }
}
